package com.skywolf.chem.usercenterapi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.skywolf.chem.common.sysconfig.response.RespData;
import com.skywolf.chem.usercenterapi.service.WxService;
import com.skywolf.chem.utils.SysConfig;
import com.skywolf.chem.wechat.MD5SHA1Utils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by pei_pei on 2020/11/10.
 */@Slf4j
@RestController
@Api(value = "微信配置签名等信息接口", description = "微信配置签名等信息接口")
@RequestMapping("/ct/wx")
public class WxController {
    //加密的随机字符串
    private String nonceStr = "a1@]73bnt]gb2312";

    @Autowired
    WxService wxService;

    @ApiOperation(value = "微信签名",notes = "微信签名")
    @GetMapping("/wechatSign")
    public RespData<String> wechatSign(HttpServletRequest request, HttpServletResponse response) {
        String token = wxService.getAccessToken();
        String jsticket = wxService.getJsapiTicket(token);
        log.info("WxController.wechatSign  token:{}  jsticket:{}", token,jsticket);
        String appid = SysConfig._this.getProperty("wechatgz.appid");

        JSONObject obj = new JSONObject();
        StringBuilder sb = new StringBuilder();
        Long timestamp = System.currentTimeMillis()/1000;
//        String queryString = StringUtils.isEmpty(request.getQueryString())?"":"?"+request.getQueryString();
        String requestUrl = "https://wap.sctynews.cn/";//request.getRequestURL().toString() + queryString;
        sb.append("jsapi_ticket=").append(jsticket).append("&noncestr=").append(nonceStr).append("&timestamp=")
                .append(timestamp).append("&url=").append(requestUrl);
        String signature = MD5SHA1Utils.SHA1(sb.toString());
        obj.put("signature",signature);
        obj.put("timestamp",timestamp);
        obj.put("nonceStr",nonceStr);
        obj.put("appId",appid);
        return RespData.success(obj);
    }
}
